package com.skin.admin.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.skin.admin.common.FenyeSetting;
import com.skin.admin.entity.Fenye;
import com.skin.admin.entity.Permissions;
import com.skin.admin.entity.Roles;
import com.skin.admin.manager.PermManager;
import com.skin.admin.manager.RoleManager;
import com.skin.configuration.shiro.ShiroLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author skin
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/perm")
public class PermController {

    @Autowired
    private PermManager permManager;

    @Autowired
    private ShiroLoad shiroLoad;

    @RequestMapping(value = "/getAll")
    public ModelAndView getAll(HttpServletRequest request){
        Fenye f= FenyeSetting.setfenye(request);
        ModelAndView mv =new ModelAndView();
        mv.addObject("perm",permManager.All(f.getCurrent(),f.getSize()));
        mv.setViewName("admin/perm/perm");
        return mv;
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable int id){
        permManager.deleteById(id);
        return "redirect:/perm/getAll";
    }

    /**
     * 编辑
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable String id,Model model){
        Permissions permissions =permManager.findBy("id",id);
        if(permissions!=null){
            model.addAttribute("perm",permissions);
        }
        return "admin/perm/permedit";
    }

    @RequestMapping(value = "/editDesc")
    public String editDesc(@RequestParam String id,Model model){
        Permissions permissions =permManager.findBy("id",id);
        if(permissions!=null){
            model.addAttribute("perm",permissions);
        }
        return "admin/perm/permeditdesc";
    }
    /**
     * 编辑
     * @param permissions
     * @return
     */
    @RequestMapping(value = "/editDo")
    public String editDo(Permissions permissions){
        permManager.edit(permissions);
        shiroLoad.updatePermission();
        return "redirect:/perm/getAll";
    }
    /**
     * 新增
     */
    @RequestMapping(value = "/insert")
    public String insert(){
        return "admin/perm/perminsert";
    }

    @RequestMapping(value = "/insertDo")
    public String insertDo(Permissions permissions){
        permManager.insert(permissions);
        shiroLoad.updatePermission(); //及时更新权限
        return "redirect:/perm/getAll";
    }
    /**
     * 查询重复
     */
    @RequestMapping(value = "/getrepetition", method = RequestMethod.GET)
    @ResponseBody
    public Integer getrepetition(@RequestParam("url") String url){
       List<Permissions> size= permManager.findListBy("url",url);
       if(size.size()>0){
           return 0;
       }
       return 1;
    }

    /**
     * 为角色分配资源
     */
    @RequestMapping(value = "/rolesconfig", method = RequestMethod.GET)
    public ModelAndView rolesconfig(@RequestParam("id") String id){
        ModelAndView mv =new ModelAndView();
        mv.addObject("perms",permManager.All());
        mv.addObject("roleid",id);
        mv.addObject("hasperms",permManager.findPermByRole(id));
        mv.setViewName("admin/perm/rolesconfig");
        shiroLoad.updatePermission(); //及时更新权限
        return mv;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/rolesconfigDo", method = RequestMethod.POST)
    @ResponseBody
    public int rolesconfigDo(@RequestParam("perms") String perms,@RequestParam("roleid") String roleid){
        int i =permManager.editPermByRoleid(perms,roleid);
        if(i==1){
            shiroLoad.updatePermission();
        }
        return  i;
    }

}

